package com.study.spmongodb.book.wwj.char08;

/**
 * @author MI
 * @version 1.0
 * @date 2020/11/25 22:38
 */
@FunctionalInterface
public interface DenyPolicy {
    void reject(Runnable runnable, ThreadPool threadPool);

    /**
     * 该拒绝策略会直接将任务丢弃
     */
    class DiscardDenyPoilcy implements DenyPolicy {
        @Override
        public void reject(Runnable runnable, ThreadPool threadPool) {

        }
    }

    /**
     * 该拒绝策略会想任务提交者抛出异常
     */
    class AbortDenyPoilcy implements DenyPolicy {
        @Override
        public void reject(Runnable runnable, ThreadPool threadPool) {
            throw new RunnableDenyException("the runnable " + runnable + " will be abort");
        }
    }

    /**
     * 该拒绝策略会使线程在提交者所在的线程中执行任务
     */
    class RunnerDenyPlicy implements DenyPolicy {
        @Override
        public void reject(Runnable runnable, ThreadPool threadPool) {
            if (!threadPool.isShutdown()) {
                runnable.run();
            }
        }
    }
}
